package com.example.rocketmqp1.controller;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.hibernate.validator.internal.util.logging.Log_$logger;
import org.springframework.stereotype.Component;

/**
 * @author CHENTAIWEN
 * @create 2022/8/8 15:09
 */
@Component
public class SpringProducer {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 创建消息生产者, 指定生产者所属的组名
        DefaultMQProducer producer = new DefaultMQProducer("my-group");
        // 指定Nameserver地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 启动生产者
        producer.start();
        // 创建消息对象，指定主题、标签和消息体
        Message msg = new Message("myTopic", "*", ("RocketMQ Message").getBytes());
        // 发送消息
        SendResult sendResult = producer.send(msg, 1000);
        System.out.println(sendResult);
        // 关闭生产者
        producer.shutdown();
    }


}
